package com.vgooo.util;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;

import android.app.Activity;

public class RequestService {

	String url = null;

	List<NameValuePair> params = new ArrayList<NameValuePair>();

	Activity activity;
	
	boolean timeout = false;

	public RequestService(String url, Activity activity) {
		init(url, activity, false);
	}

	public RequestService(String url, Activity activity, boolean timeout) {
		init(url, activity, timeout);
	}

	public void init(String url, Activity activity, boolean timeout) {
		this.url = url;
		this.activity = activity;
		this.timeout = timeout;

		BasicHttpParams httpParameters = null;
		if (timeout) {
			httpParameters = new BasicHttpParams();
			// Set the default socket timeout (SO_TIMEOUT)
			HttpConnectionParams.setConnectionTimeout(httpParameters, 3000);
			// in milliseconds which is the timeout for waiting for data.
			HttpConnectionParams.setSoTimeout(httpParameters, 3000);
		}

		request = new HttpPost(url);

		defaultHttpClient = new DefaultHttpClient(httpParameters);		
	}

	public void setParams(List<NameValuePair> params) {
		this.params = params;
	}

	public void addParam(String key, String value) {
		NameValuePair pair = new BasicNameValuePair(key, value);

		this.params.add(pair);
	}

	public void addParam(BasicNameValuePair pair) {
		this.params.add(pair);
	}

	public void doGet() {

	}

	public DefaultHttpClient defaultHttpClient;

	private HttpPost request = null;
	private HttpResponse response = null;
	private String result = null;
	
	private int readTimes = 0;

	public void doPost() throws Exception {
		try {
			request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
			response = defaultHttpClient.execute(request);

			if (response.getStatusLine().getStatusCode() == 200) {
				result = EntityUtils.toString(response.getEntity());

				if (result.equals("")) {
					result = null;
				}
			}
		} catch (Exception e) {
			if (readTimes > 10) {
				return;
			}
			
			if (!timeout && activity != null && Utils.hasNetWork(activity)) {
				readTimes++;
				doPost();
			}else {
				throw e;
			}
			e.printStackTrace();
		}
	}

	public String getString() {
		return result;
	}

	public ArrayList<Map<String, Object>> getArrayList() throws JSONException {
		if (result != null && result.length() > 0) {
			ArrayList<Map<String, Object>> arrayList = Utils
					.getArrayList(result);
			if (arrayList.size() < 1) {
				return null;
			}
			return arrayList;
		}
		return null;
	}
}
